using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GridLine : Graphic
{
    [Space]
    [Range(0, 100)]
    public int width = 9;
    [Range(0, 100)]
    public int height = 9;
    [Range(1, 10)]
    public int horizontalGroupSize = 3;
    [Range(1, 10)]
    public int verticalGroupSize = 3;

    public float lineSize = 1f;
    public float groupLineSize = 3f;
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        var (bl, tr) = UIMeshHelper.CalculateCorners(rectTransform);

        vh.Clear();

        var cellWidth = rectTransform.rect.width / width;
        var cellHeight = rectTransform.rect.height / height;

        // Draw horizontal lines.
        for (int i = 1; i < height; i++)
        {
            float currentSize = i % verticalGroupSize == 0 ? groupLineSize : lineSize;

            UIMeshHelper.DrawLine(vh, new Vector2(bl.x, bl.y + i * cellHeight), new Vector2(tr.x, bl.y + i * cellHeight), currentSize, color);
        }

        // Draw vertical lines.
        for (int i = 1; i < width; i++)
        {
            float currentSize = i % horizontalGroupSize == 0 ? groupLineSize : lineSize;

            UIMeshHelper.DrawLine(vh, new Vector2(bl.x + i * cellWidth, bl.y), new Vector2(bl.x + i * cellWidth, tr.y), currentSize, color);
        }

        // Draw out frame
        // Left side
        UIMeshHelper.DrawRect(vh, bl, new Vector2(bl.x + groupLineSize, tr.y), color);
        // Right side
        UIMeshHelper.DrawRect(vh, new Vector2(tr.x - groupLineSize, bl.y), tr, color);
        // Top side
        UIMeshHelper.DrawRect(vh, new Vector2(bl.x + groupLineSize, tr.y - groupLineSize), new Vector2(tr.x - groupLineSize, tr.y), color);
        // Bottom side
        UIMeshHelper.DrawRect(vh, new Vector2(bl.x + groupLineSize, bl.y), new Vector2(tr.x - groupLineSize, bl.y + groupLineSize), color);
    }
}
